package com.huihui.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.huihui.MainApp;
import com.huihui.R;
import com.huihui.datamanager.AppManager;
import com.huihui.network.pojo.UserInfo;
import com.huihui.widget.SwipeRecyclerView;
import com.huihui.widget.dialog.LoadingDialogCircle;
import com.huihui.widget.footerView.SimpleFooterView;

import java.util.UUID;

public class BaseActivity extends AppCompatActivity {
    protected final String TAG = getClass().getSimpleName();
    private Context mContext;
    private LoadingDialogCircle mLoadingDialogCircle;
    private Toolbar mActionBarToolbar;
    private View mParentView;
    protected final Handler UIHandler = new Handler();
    private UserInfo userInfo;
    protected TextView titleView;

    public UserInfo getUserInfo() {

        userInfo = AppManager.getUserInfo();

        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);
        }
    }
    protected  void uploadFileSuccess(){

    }
    @Override
    public void setContentView(int layoutResID) {
        mParentView = getLayoutInflater().inflate(layoutResID, null);
        super.setContentView(mParentView);
        getActionBarToolbar();
    }

    public Context getContext() {
        return mContext;
    }

    protected Toolbar getActionBarToolbar() {
        if (mActionBarToolbar == null) {
            mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
            if (mActionBarToolbar != null) {
                mActionBarToolbar.setTitle("");
                titleView = findViewById(R.id.toolbar_title);
                setSupportActionBar(mActionBarToolbar);
                getSupportActionBar().setTitle("");
                mActionBarToolbar.setNavigationIcon(R.drawable.icon_common_backpressed_selecter);

                mActionBarToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
            }
        }
        return mActionBarToolbar;
    }

    public  String getUUID32() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }


    public  void uploadFile(String objectKey, String uploadFilePath) {
        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(MainApp.stsToken.getBucketName(), objectKey, uploadFilePath);
        // 异步上传时可以设置进度回调
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
            }
        });
        OSSAsyncTask task = MainApp.oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                Log.d("aliyun", "success");
                uploadFileSuccess();
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("aliyun", "error");
                }
            }
        });
    }

    public void setRecycleView(SwipeRecyclerView recyclerView) {
        //set layoutManager
        recyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        //设置emptyView
//        TextView textView = new TextView(this);
//        textView.setText("没有更多数据");
//        recyclerView.setEmptyView(textView);

        //设置footerView
//        recyclerView.setFooterView(new SimpleFooterView(mContext));
//        //设置去除footerView 的分割线
//        recyclerView.getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
//            @Override
//            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//                super.onDraw(c, parent, state);
//                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//                paint.setColor(0xFFEECCCC);
//
//                Rect rect = new Rect();
//                int left = parent.getPaddingLeft();
//                int right = parent.getWidth() - parent.getPaddingRight();
//                final int childCount = parent.getChildCount() - 1;
//                for (int i = 0; i < childCount; i++) {
//                    final View child = parent.getChildAt(i);
//
//                    //获得child的布局信息
//                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//                    final int top = child.getBottom() + params.bottomMargin;
//                    final int itemDividerHeight = 1;//px
//                    rect.set(left + 50, top, right - 50, top + itemDividerHeight);
//                    c.drawRect(rect, paint);
//                }
//            }
//        });
    }

    // 显示输入法
    public static void showInput(Context context, View focusView) {
        focusView.requestFocus();
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(focusView, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
    public static void showInput2(Context context,final View focusView) {
        focusView.requestFocus();
        final InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        focusView.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) focusView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }

    // 隐藏输入法
    public static void hideInput(Context context) {
        View view = ((Activity) context).getWindow().peekDecorView();
        if (view != null && view.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 转新菊花
     */
    public LoadingDialogCircle showLoadingDialog() {
        if (mLoadingDialogCircle == null) {
            mLoadingDialogCircle = new LoadingDialogCircle(mContext);
        }
        mLoadingDialogCircle.showLoading();
        return mLoadingDialogCircle;
    }

    /**
     * 隐藏新菊花
     */
    public void hideLoadingDialog() {
        if (mLoadingDialogCircle != null) {
            mLoadingDialogCircle.hideLoading();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            View v = getCurrentFocus();
//            if (isShouldHideInput(v, ev)) {
//
//                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                if (imm != null) {
//                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//                }
//            }
//            return super.dispatchTouchEvent(ev);
//        }
//        // 必不可少，否则所有的组件都不会有TouchEvent了
//        if (getWindow().superDispatchTouchEvent(ev)) {
//            return true;
//        }
//        return onTouchEvent(ev);
//    }
//
//
//    public boolean isShouldHideInput(View v, MotionEvent event) {
//        if (v != null && (v instanceof EditText)) {
//            int[] leftTop = {0, 0};
//            //获取输入框当前的location位置
//            v.getLocationInWindow(leftTop);
//            int left = leftTop[0];
//            int top = leftTop[1];
//            int bottom = top + v.getHeight();
//            int right = left + v.getWidth();
//            if (event.getX() > left && event.getX() < right
//                    && event.getY() > top && event.getY() < bottom) {
//                // 点击的是输入框区域，保留点击EditText的事件
//                return false;
//            } else {
//                return true;
//            }
//        }
//        return false;
//    }
}
